package cn.anlab.anappframe.widget;

import cn.anlab.anappframe.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 数据列表组件 <br/>
 * 在原有的基础上，增加头行滚动分页、底部点击按钮分页的支持
 * 
 * @version 1.0
 * @author Stan Rong 2012-3-3
 */
public class CompListView extends ListView {
	
	private Context context;
	
	/**
	 * 属性：头行滚动加载
	 */
	public static final int PROP_HEADER_LOADING =1 << 0;
	
	/**
	 * 属性：底部点击MORE按钮加载
	 */
	public static final int PROP_FOOTER_LOADING =1 << 1;
	
	//头部状态
	public static final int HEADER_STATE_GONE = 1;						//隐藏状态
	public static final int HEADER_STATE_TO_RELEASE = 2;			//等待释放状态
	public static final int HEADER_STATE_LOADING = 3;				//加载中状态
	
	public static final int FOOTER_STATE_GONE = 1;						//隐藏状态
	public static final int FOOTER_STATE_MORE_BUTTON = 2;		//更多按钮状态
	public static final int FOOTER_STATE_LOADING = 3;				//加载中状态
	
	private int properties = 0;
	
	LinearLayout headerView;			//头行（加载中）
	LinearLayout footerView;			//尾行（查看更多）

	/**
	 * 构造方法 
	 * @param context
	 * @param properties 属性，如CompListView.PROP_HEADER_LOADING | CompListView.PROP_FOOTER_LOADING
	 */
	public CompListView(Context context, int properties) {
		super(context);
		this.context = context;
		this.properties = properties;
		setHeaderAndFooter();
	}
	
	public CompListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompListView);
		
		setHeaderAndFooter();
	}
	
	
//	TypedArray a = context.obtainStyledAttributes(attrs,     
//            R.styleable.MyView);     
//         
//    int textColor = a.getColor(R.styleable.MyView_textColor,     
//            0XFFFFFFFF);     
//    float textSize = a.getDimension(R.styleable.MyView_textSize, 36);     
//         
//    mPaint.setTextSize(textSize);     
//    mPaint.setColor(textColor);     
//         
//    a.recycle();     
//	
	
	/**
	 * 设置头行和尾行视图
	 */
	private void setHeaderAndFooter() {
		if(hasProperty(PROP_HEADER_LOADING)) {
			addHeaderView();
		}
		if(hasProperty(PROP_FOOTER_LOADING)) {
			addFooterView();
		}
	}
	
	/**
	 * 显示、隐藏头行
	 */
	public void setHeaderState(int state) {
		if( ! hasProperty(PROP_HEADER_LOADING)) {
			throw new RuntimeException("Make sure you have set the property PROP_HEADER_LOADING.");
		}
		switch (state) {
		case HEADER_STATE_GONE:
			headerView.setVisibility(View.GONE);
			setItemHeight(footerView, 0);
			break;
		case HEADER_STATE_TO_RELEASE:
			headerView.setVisibility(View.VISIBLE);
			setItemHeight(footerView, 45);
			ivLoading.setImageDrawable(getResources().getDrawable(R.drawable.up));
			tvLoading.setText("release to load...");
			break;
		case HEADER_STATE_LOADING:
			headerView.setVisibility(View.VISIBLE);
			setItemHeight(footerView, 30);
			ivLoading.setImageDrawable(getResources().getDrawable(R.drawable.loading_small1));
			tvLoading.setText("loading...");
			break;	
		}
	}
	
	
	/**
	 * 显示、隐藏尾行
	 */
	public void setFooterState(int state) {
		if( ! hasProperty(PROP_FOOTER_LOADING)) {
			throw new RuntimeException("Make sure you have set the property PROP_FOOTER_LOADING.");
		}
		switch (state) {
		case FOOTER_STATE_GONE:
			footerView.setVisibility(View.GONE);
			setItemHeight(footerView, 0);
			break;
		case FOOTER_STATE_MORE_BUTTON:
			footerView.setVisibility(View.VISIBLE);
			setItemHeight(footerView, AbsListView.LayoutParams.WRAP_CONTENT);
			btnMore.setText("MORE");
			break;
		case FOOTER_STATE_LOADING:
			footerView.setVisibility(View.VISIBLE);
			setItemHeight(footerView, AbsListView.LayoutParams.WRAP_CONTENT);
			btnMore.setText("loading...");
			break;	
		}
	}
	
	private void setItemHeight(View iView, int height) {
		AbsListView.LayoutParams iViewPrm = (AbsListView.LayoutParams)iView.getLayoutParams(); 	
		if(iViewPrm == null) {
			iViewPrm = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
		} else {
			iViewPrm.height = AbsListView.LayoutParams.WRAP_CONTENT;
		}
	}
	
	/**
	 * 添加ListView头行：加载中的图标及文字描述
	 */
	private void addHeaderView() {
		headerView = new LinearLayout(context);
		headerView.setGravity(Gravity.CENTER);
//		headerView.setPadding(0, 3, 0, 3);
		headerView.setVisibility(View.GONE);
		
		//加载中图标
		ivLoading = new ImageView(context);
		ivLoading.setImageResource(R.drawable.loading_small1);
		LinearLayout.LayoutParams prm = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		headerView.addView(ivLoading, prm);
		
		//加载中文字描述
		tvLoading = new TextView(context);
		tvLoading.setText("Loading...");
		prm = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		headerView.addView(tvLoading, prm);
		
		AbsListView.LayoutParams itemPrm = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, 0);
		headerView.setLayoutParams(itemPrm);
		//添加到ListView头行
		addHeaderView(headerView);
	}
	private TextView tvLoading; 
	private ImageView ivLoading;
	
	/**
	 * 添加ListView尾行：加载底部
	 */
	private void addFooterView() {
		footerView = new LinearLayout(context);
		footerView.setGravity(Gravity.CENTER);
		footerView.setVisibility(View.GONE);
		
		//加载按钮
		btnMore = new Button(context);
		btnMore.setText("More...");
		btnMore.setBackgroundResource(R.drawable.btn_default);
		LinearLayout.LayoutParams prm = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		footerView.addView(btnMore, prm);
		
		//添加到ListView头行
		addFooterView(footerView);
	}	
	private Button btnMore;
	
	/**
	 * 判断当前ListView是否具备某属性
	 * @param property
	 * @return
	 */
	public boolean hasProperty(int property) {
		if((properties & property) == property) {
			return true;
		} else {
			return false;
		}
	}	
	

}
